import Mock from 'mockjs';

// 全局变量存储用户信息
let currentUser = null;
let mockAdmins = Mock.mock({
    'list|12': [{  // 修改为12个管理员
        id: '@id',
        username: '@cname',
        'role|2': [1, 2], // 1-管理员 2-超级管理员
        email: '@email',
        gender: '@pick(["男", "女"])',
        createtime: '@datetime("yyyy-MM-dd HH:mm:ss")'  // 格式化时间
    }]
}).list;

export default {
    getUserInfo: () => {
        if (currentUser) {
            return {
                "status": true,
                "code": 0,
                "message": null,
                "data": currentUser,
                "otherData": {}
            };
        }

        const storedUser = sessionStorage.getItem('userInfo');
        if (storedUser) {
            currentUser = JSON.parse(storedUser);
            return {
                "status": true,
                "code": 0,
                "message": null,
                "data": currentUser,
                "otherData": {}
            };
        }

        return {
            "status": true,
            "code": 0,
            "message": null,
            "data": {
                "id": 1,
                "username": "admin",
                "role": 1,
                "email": "admin@example.com",
                "gender": "男",
                "avatar": null
            },
            "otherData": {}
        }
    },

    userLogin: (data) => {
        const users = [
            {
                username: "superadmin",
                password: "superadmin",
                user: {
                    id: 0,
                    username: "superadmin",
                    role: 2,
                    email: "superadmin@example.com",
                    gender: "男",
                    avatar: null
                }
            },
            {
                username: "admin",
                password: "admin",
                user: {
                    id: 1,
                    username: "admin",
                    role: 1,
                    email: "admin@example.com",
                    gender: "男",
                    avatar: null
                }
            },
            {
                username: "userr",
                password: "userr",
                user: {
                    id: 2,
                    username: "user",
                    role: 0,
                    email: "user@example.com",
                    gender: "男",
                    avatar: null
                }
            }
        ];

        const foundUser = users.find(u => u.username === data.username && u.password === data.password);
        if (foundUser) {
            currentUser = foundUser.user;
            sessionStorage.setItem('userInfo', JSON.stringify(foundUser.user));
            return {
                status: true,
                code: 0,
                message: "登录成功",
                data: foundUser.user,
                "otherData": {}
            };
        } else {
            return {
                status: false,
                code: 1,
                message: "用户名或密码错误",
                data: null,
                "otherData": {}
            };
        }
    },
// 新增 getUserList 方法
    getUserList: () => {
        return Mock.mock({
            status: true,
            code: 0,
            message: null,
            data: {
                total: 10, // 假设总共有10条记录
                'records|5': [
                    {
                        id: '@id',
                        username: '@cname',
                        role: '@integer(1, 2)',
                        email: '@email',
                        gender: '@pick(["男", "女"])',
                        createtime: '@datetime'
                    }
                ]
            }
        });
    },
    // 新增删除用户的Mock方法
    deleteUser: () => {
        return {
            status: true,
            code: 0,
            message: '用户删除成功',
            data: null
        };
    },
    // 新增更新用户的Mock方法
    updateUser: () => {
        return {
            status: true,
            code: 0,
            message: '用户信息更新成功',
            data: null
        };
    },
    // 新增添加用户的Mock方法
    insertUser: () => {
        return {
            status: true,
            code: 0,
            message: '用户添加成功',
            data: null
        };
    },

    // 获取管理员列表
    getAdminList: (params) => {
        const { username = '', page = 1, size = 10 } = params;
        let filtered = mockAdmins.filter(admin =>
            admin.username.includes(username)
        );

        const total = filtered.length;
        const start = (page - 1) * size;
        const end = start + size;
        const records = filtered.slice(start, end);

        return {
            status: true,
            code: 0,
            message: null,
            data: {
                total,
                records
            }
        };
    },

    // 添加管理员
    addAdmin: (data) => {
        const newAdmin = {
            id: Mock.mock('@id'),
            ...data,
            role: 1, // 新增用户默认为管理员
            createtime: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")')
        };
        mockAdmins.unshift(newAdmin); // 添加到数组开头
        return {
            status: true,
            code: 0,
            message: '管理员添加成功',
            data: newAdmin
        };
    },

    // 更新管理员
    updateAdmin: (id, data) => {
        const index = mockAdmins.findIndex(admin => admin.id === id);
        if (index !== -1) {
            mockAdmins[index] = { ...mockAdmins[index], ...data };
            return {
                status: true,
                code: 0,
                message: '管理员更新成功',
                data: mockAdmins[index]
            };
        }
        return {
            status: false,
            code: 1,
            message: '管理员不存在',
            data: null
        };
    },

    // 删除管理员
    deleteAdmin: (id) => {
        const initialLength = mockAdmins.length;
        mockAdmins = mockAdmins.filter(admin => admin.id !== id);
        return {
            status: true,
            code: 0,
            message: initialLength !== mockAdmins.length ? '管理员删除成功' : '管理员不存在',
            data: null
        };
    }
}